package com.hdit.guimei.service.impl;

import com.hdit.guimei.domain.User;
import com.hdit.guimei.mapper.UserMapper;
import com.hdit.guimei.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @基本功能:
 * @ClassName: UserServiceImpl
 * @Description: TODO
 * @Author: 李嘉明
 * @Date: 2021/1/18 10:21
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    /**
     * @Method: userRegister
     * @函数(方法)功能: 用户注册
     * @Author: 李嘉明
     * @params: [user]
     * @Return: int
     * @Exception:
     * @Date: 2021/1/18 13:34
     */
    public int userRegister(User user) {
        return userMapper.addUser(user);
    }

    @Override
    public Map userLogin(String user_name, String user_pwd) {
        User user = userMapper.findUserByName(user_name);
        Map map=new HashMap();
        //查询的用户不存在
        if (user == null) {
            map.put("code",404);
            map.put("msg","用户名不存在");
            return map;
        }
        //用户密码正确，登录成功返回1
        if (user_pwd.equals(user.getUser_pwd())) {
            map.put("code",200);
            map.put("msg","登录成功");
            map.put("user",user);
            return map;
        }
        //用户输入的密码错误，返回-1
        map.put("code",500);
        map.put("msg","密码存储");
        return map;
    }
    @Override
    public int updateUserPwd(String user_name, String user_pwd, String new_user_pwd) {
        User user = userMapper.findUserByName(user_name);
        if(user==null){
            return 2;//修改的用户不存在
        }
        if (user_pwd.equals(user.getUser_pwd())){
            Map map = new HashMap<String,String>();
            map.put("user_name",user_name);
            map.put("new_user_pwd",new_user_pwd);
            return userMapper.updateUserPwd(map);
        }
        return -1; //要修改的用户，输入的旧密码错误
    }
    /**
     *@methodName  userupdate
     *@方法功能描述：修改用户信息 修改成功返回1  错误返回-1
     *@params  [user]
     *@return  int
     *@author   张宝玉
     *@Date    2021/1/18 16:51
     */
    @Override
    public int userupdate(User user) {
        int updateuser = userMapper.updateuser(user);
        return updateuser;
    }
    /***
    * @Method 注册时查询用户名是否存在
    * @Author 冰镇小可乐
    * @params  [username]
    * @Return java.util.List<com.hdit.guimei.domain.User>
    * @Exception
    * @Date 2021/1/26 8:51
    */
    @Override
    public Map findUserByUsername(String user_name) {
        Map map=new HashMap();
        List<User> users=userMapper.findUserByUserName(user_name);
        System.out.println(users);
        if (users.size()>0){
            map.put("code","002");
        }else {
            map.put("code","001");
        }
        return map;
    }

    @Override
    public List<User> findAllUsers() {
        return userMapper.findAllUser();
    }

    @Override
    public int updateUserState(int id,int user_lock) {
        Map<String , Integer> map = new HashMap();
        map.put("id",id);
        map.put("user_lock",user_lock);
        return userMapper.updateUserState(map);
    }
}
